package ui;

import java.io.File;

import application.ApplicationModel;

import com.vaadin.annotations.Theme;
import com.vaadin.server.FileResource;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinService;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;


@SuppressWarnings("serial")
@Theme("demoopenis")
public class DemoopenisUI extends UI {
	
	private ApplicationModel application = new ApplicationModel();
	
	
	private VerticalLayout topLayout = new VerticalLayout();

	private TextField searchField = new TextField();
	private Button searchButton = new Button("Search");
	
	private Panel mainView = new Panel();


	@Override
	protected void init(VaadinRequest request) {
		setContent(topLayout);
		topLayout.setSpacing(true);
		
		initSearchLayout();
		initMainLayout();
	}
	
	
	private void initSearchLayout() {
		HorizontalLayout layout = new HorizontalLayout();
		topLayout.addComponent(layout);
		
		layout.addComponent(searchField);
		layout.setComponentAlignment(searchField, Alignment.MIDDLE_CENTER);
		layout.addComponent(searchButton);
		
		searchField.setInputPrompt("Search celebrities");
		searchField.setWidth("100%");
		
		layout.setWidth("100%");
		layout.setExpandRatio(searchField, 1);
		
		searchButton.setIcon(new FileResource(new File(
				VaadinService.getCurrent().getBaseDirectory(),
				"/WEB-INF/icons/search16.png"
				)));
		searchButton.addClickListener(
				new ClickListener() {
					@Override
					public void buttonClick(ClickEvent event) {
						
						searchCelebrity();
						
					}
				}
				);
	}
	
	private void initMainLayout() {	
		topLayout.setSizeFull();
		
		topLayout.addComponent(mainView);
		
		mainView.setSizeFull();
		topLayout.setExpandRatio(mainView, 1);
		
		mainView.addStyleName(Runo.PANEL_LIGHT);
		
		mainView.setContent(new WelcomeView());
	}
	
	
	private void searchCelebrity() {
		String query = searchField.getValue();
		
		mainView.setContent(
				new ResultsView(application, query){
					@Override
					protected void setMainLayout(Layout layout) {
						
						mainView.setContent(layout);
						
					}
				}
				);
	}

}